import { ref } from 'vue'
import { getHomeData, delAbstract } from '@/network/api/home'
import { ElMessage } from 'element-plus'

export const useInitEffect = () => {
    // 发送请求 初始化数据
    const blogData = ref([])
    const count = ref(null)
    const getDataFunc = async (params = { currentPage: 1, pageSize: 5 }) => {
        const res = await getHomeData(params)
        count.value = res.result.count
        const data = res?.result?.result?.map(item => {
            return {
                id: item.id,
                abstract: item.article,
                title: item.title,
                contentId: item.content_id,
                topImg: item.top_img,
                tags: item.category_arr
            }
        }) || []
        // console.log(data)
        blogData.value = data
        // console.log(blogData)
    }
    getDataFunc()
    return {
        blogData,
        count,
        getDataFunc
    }
}
// 根据contentId 删除\编辑
export const useEditEffect = (router) => {
    const handleEdit = (contentId) => {
        router.push({
            path: 'edit',
            query: {
                contentId
            }
        })
        console.log(contentId)
    }
    return {
        handleEdit
    }
}

export const useDeleteEffect = () => {
    const handleDelete = (contentId, func) => {
        delAbstract({ contentId }).then(res => {
            if (res.status === 200) {
                ElMessage.success('操作成功')
                func()
            }
        })
    }
    return {
        handleDelete
    }
}

